route.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { getServerSideConfig } from "@/app/config/server";
  2. import { ModelProvider } from "@/app/constant";
  3. import { prettyObject } from "@/app/utils/format";
  4. import { NextRequest, NextResponse } from "next/server";
  5. import { auth } from "../../auth";
  6. import { requestOpenai } from "../../common";
  7. async function handle(
  8. req: NextRequest,
  9. { params }: { params: { path: string[] } },
  10. ) {
  11. console.log("[Azure Route] params ", params);
  12. if (req.method === "OPTIONS") {
  13. return NextResponse.json({ body: "OK" }, { status: 200 });
  14. }
  15. const subpath = params.path.join("/");
  16. const authResult = auth(req, ModelProvider.GPT);
  17. if (authResult.error) {
  18. return NextResponse.json(authResult, {
  19. status: 401,
  20. });
  21. }
  22. try {
  23. return await requestOpenai(req);
  24. } catch (e) {
  25. console.error("[Azure] ", e);
  26. return NextResponse.json(prettyObject(e));
  27. }
  28. }
  29. export const GET = handle;
  30. export const POST = handle;
  31. export const runtime = "edge";
  32. export const preferredRegion = [
  33. "arn1",
  34. "bom1",
  35. "cdg1",
  36. "cle1",
  37. "cpt1",
  38. "dub1",
  39. "fra1",
  40. "gru1",
  41. "hnd1",
  42. "iad1",
  43. "icn1",
  44. "kix1",
  45. "lhr1",
  46. "pdx1",
  47. "sfo1",
  48. "sin1",
  49. "syd1",
  50. ];